//
//  MyRewardViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/22.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "MyRewardTableViewCell.h"
#import "MyRewardViewController.h"
#import "MyRewardModel.h"

@interface MyRewardViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * mainTableView;
@property (nonatomic,strong) UISegmentedControl * segment;
@property (nonatomic,strong) UIView * lineView;
@property (nonatomic,strong)NSMutableArray * dataSource;
@property (nonatomic,assign)BOOL    isRecive;//是否领取
@end

@implementation MyRewardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = GRAYCOLOR;
    self.title = @"我的奖励";
    _dataSource = [NSMutableArray array];
    [self.view addSubview:self.segment];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.mainTableView];
    [self segmentValueDidChange:_segment];
    
}
-(void)buttonReciveRewardTapped:(UIButton *)sender
{
    //领取奖励
}
-(void)requestTheDataWithIsRecive:(BOOL)isRecive
{
    [_dataSource removeAllObjects];
    NSString *timestamp=[GeneralTool gettimestamp];
    
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,@"ColorEggsStatus":[NSString stringWithFormat:@"%d",isRecive],@"Timestamp":timestamp};//1 代表已领取
    NSString *sign=[GeneralTool GetSin:dic];

    NSString *urlString=[NSString stringWithFormat:@"Activity/ColorEggsList/%@/%@/%@/%@",GETUSERCODE,[NSString stringWithFormat:@"%d",isRecive],timestamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        if ([responses[@"IsSuccess"]intValue]==1) {
            NSArray * dataArr = [responses objectForKey:@"ReturnObject"];
            if(![dataArr isKindOfClass:[NSNull class]])
            {
                for (NSDictionary * rewardDic in dataArr)
                {
                    MyRewardModel * model = [[MyRewardModel alloc]init];
                    [model setValuesForKeysWithDictionary:rewardDic];
                    [_dataSource addObject:model];
                }
                if (dataArr.count<15)
                {
                    [_mainTableView.mj_footer endRefreshingWithNoMoreData];
                }else
                {
                    [_mainTableView.mj_footer endRefreshing];
                }
            }
            
            [_mainTableView.mj_footer endRefreshing];
           
        }else
        {
            [MBProgressHUD showError:@"获取信息失败"];
        }
        [_mainTableView reloadData];
        [_mainTableView.mj_header endRefreshing];
        if(_dataSource.count==0)
        {
            //显示没有奖励
            
        }
    }];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [GeneralTool showTabBar];
}
-(void)segmentValueDidChange:(UISegmentedControl *)seg
{
    _lineView.frame = CGRectMake(_segment.selectedSegmentIndex*kWidth/2.0, CGRectGetMaxY(_segment.frame), kWidth/2.0 , 2);
    
    if (seg.selectedSegmentIndex == 0)
    {
        _isRecive = YES;
    }else
    {
        _isRecive = NO;
    }
    [self requestTheDataWithIsRecive:_isRecive];
}
-(UITableView *)mainTableView
{
    if (!_mainTableView)
    {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 2+44+20+64,kWidth, kHeight-CGRectGetMaxY(_lineView.frame)-20) style:UITableViewStylePlain];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_mainTableView registerClass:[MyRewardTableViewCell class] forCellReuseIdentifier:@"cell"];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.backgroundColor = GRAYCOLOR;
        _mainTableView.tableFooterView = [UIView new];//设置当数据不多的时候，下面无cell
        _mainTableView.sectionHeaderHeight = 0;
        _mainTableView.sectionFooterHeight = 0;
    }
    return _mainTableView;
}
-(UISegmentedControl *)segment
{
    if (!_segment)
    {
        _segment = [[UISegmentedControl alloc]initWithItems:@[@"已领取",@"未领取"]];
        _segment.backgroundColor = [UIColor whiteColor];
        _segment.frame = CGRectMake(0, 65, kWidth, 44);
        _segment.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName:BLUECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateSelected];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
        _segment.tintColor = [UIColor whiteColor];
        [_segment addTarget:self action:@selector(segmentValueDidChange:) forControlEvents:UIControlEventValueChanged];
        _segment.selectedSegmentIndex = 0;
    }
    return _segment;
}
-(UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = BLUECOLOR;
    }
    return _lineView;
}
#pragma mark----------delegagt--datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyRewardTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MyRewardModel * model = [_dataSource objectAtIndex:indexPath.row];
    cell.prizeSourceLabel.text = model.PrizeSource;
    cell.prizeNameLabel.text = model.PrizeName;
    cell.getTimeLabel.text = [NSString stringWithFormat:@"获取时间%@",model.GetTime];
    cell.getBtn.tag = indexPath.row;
    [cell.getBtn addTarget:self action:@selector(buttonReciveRewardTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.getBtn.hidden = _isRecive;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
